package com.wh.usercenter3.util;

import com.wh.usercenter3.constant.CodeConstant;
import com.wh.usercenter3.exception.BaseException;
import com.wh.usercenter3.pojo.Team;
import com.wh.usercenter3.pojo.User;
import com.wh.usercenter3.pojo.vo.NewTeamVo;
import com.wh.usercenter3.pojo.vo.PageDataVo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TeamUtil {
    /**
     * team前端数据合法性封装校验
     *
     * @param team
     * @param request
     */
    public static void dateCheck(Team team, HttpServletRequest request) {
        //1. 判空参数
        if (team == null) throw new BaseException(CodeConstant.CLIENT_ERR, "参数为空");
        //2. 是否登录 and userId必须等于当前登录用户id
        User user = UserUtil.userByLogin(request);
        Long userId = user.getId();
        Long teamUserId = team.getUserId();
        if (user == null) throw new BaseException(CodeConstant.CLIENT_ERR, "未登录");
        teamUserId = Optional.ofNullable(teamUserId).orElse(userId);
        if (!Objects.equals(userId, teamUserId)) {
            //管理员校验
            if (!UserUtil.isAdmin(UserUtil.userByLogin(request)))
                throw new BaseException(CodeConstant.CLIENT_ERR, "权限不足");
        }
        //3. 参数合法性校验
        //   1. name <= 20
        String name = team.getName();
        if (StringUtils.isEmpty(name) || name.length() > 20)
            throw new BaseException(CodeConstant.CLIENT_ERR, "参数有误");
        //   2. desc <= 200
        String description = team.getDescription();
        if (StringUtils.isEmpty(description) || description.length() > 200)
            throw new BaseException(CodeConstant.CLIENT_ERR, "参数有误");
        //   3. maxNum <= 8 and >1
        Integer maxNum = team.getMaxNum();
        if (maxNum == null || maxNum > 8 || maxNum < 2) throw new BaseException(CodeConstant.CLIENT_ERR, "参数有误");
        //   4. expireTime > nowTime
        LocalDateTime expireTime = team.getExpireTime();
        if (expireTime == null || expireTime.isBefore(LocalDateTime.now()))
            throw new BaseException(CodeConstant.CLIENT_ERR, "时间超出");
        //   5. status if == 2 需要密码 如果没有默认为0
        Integer status = team.getStatus();
        String password = team.getPassword();
        status = Optional.ofNullable(status).orElse(0);
        if (status > 2 || status < 0) {
            throw new BaseException(CodeConstant.CLIENT_ERR, "参数有误");
        }
        if (status == 2) {
            //   6. password 必须长度等于8位并且只能有英文字母和数字
            // 正则表达式
            String regex = "^[a-zA-Z0-9]{8}$";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(password);
            if (!matcher.matches()) {
                throw new BaseException(CodeConstant.CLIENT_ERR, "密码不合规");
            }
        }
    }

    public static PageDataVo<List<NewTeamVo>> setPageDataVo(List<NewTeamVo> list,Integer pageNum,Integer pageSize){
        int listSize = list.size();

        // 判断分页是否越界
        if (((pageNum-1)*pageSize)>(listSize -1)){
            return null;
        }
        PageDataVo<List<NewTeamVo>> pageDataVo = new PageDataVo<>();

        // 前后边界赋值
        int front=((pageNum-1)*pageSize);
        int after=Math.min(((pageNum)* pageSize), listSize);

        // 分页内容截取
        List<NewTeamVo> reCords=list.subList(front,after);

        // 赋值对象字段
        pageDataVo.setRecords(reCords);
        pageDataVo.setNum(pageNum);
        pageDataVo.setSize(pageSize);
        pageDataVo.setTotal(reCords.size());
        return pageDataVo;
    }

}
